package com.notemanagement.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.notemanagement.R;
import com.notemanagement.utils.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 金 on 2015/1/17.
 */
public class Category extends Activity implements View.OnClickListener{
    private EditText editText;
    private Button button;
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> list;
    private ImageView back_image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.category);
        
        initView();
    }

    private void initView() {
        editText = (EditText) findViewById(R.id.category_edit_text);
        button = (Button) findViewById(R.id.category_button_add);
        listView = (ListView) findViewById(R.id.category_list);
        back_image = (ImageView) findViewById(R.id.category_head_back);

        button.setOnClickListener(this);
        back_image.setOnClickListener(this);
        list = Constant.db.queryProductItems();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.category_button_add:
                if(!editText.getText().toString().equals("")) {
                    Constant.db.insertProductItem(editText.getText().toString());
                    if(!list.contains(editText.getText().toString())) {
                        list.add(editText.getText().toString());
                        adapter.notifyDataSetChanged();
                    }
                    editText.setText("");
                }
                break;
            case R.id.category_head_back:
                this.finish();
                break;
        }
    }
}
